from django.db import models
from user.models import User


# Create your models here.
class Category(models.Model):
    name = models.CharField(
        max_length=100,
        verbose_name='故事分类'
    )

    class Meta:
        verbose_name = '故事分类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name

class Story(models.Model):
    title = models.CharField(
        max_length=120,
        blank=True,
        null=True,
        verbose_name='故事标题'
    )

    contents = models.TextField(
        blank=True,
        null=True,
        verbose_name='故事内容'
    )

    category = models.ForeignKey(
        Category,
        on_delete=models.CASCADE,
        verbose_name='故事分类'
    )

    date = models.DateField(
        auto_now=False,
        auto_now_add=False,
        null=True,
        blank=True,
        verbose_name='创建日期'
    )

    creator = models.ForeignKey(
        User,
        on_delete=models.CASCADE,
        verbose_name='创作者'
    )

    likes = models.ManyToManyField(
        User,
        related_name='liked_stories',  # 反向查询：用户→其点赞的故事
        blank=True,  # 允许无用户点赞
        verbose_name='点赞用户'
    )
    collects = models.ManyToManyField(
        User,
        related_name='collected_stories',  # 反向查询：用户→其收藏的故事
        blank=True,  # 允许无用户收藏
        verbose_name='收藏用户'
    )

    class Meta:
        verbose_name = '故事'
        verbose_name_plural = '故事'

    def __str__(self):
        return self.title